<?php
/**************************************
 *
 * @services 服务层~主要进行数据筛选与过滤
 *
 **************************************/
namespace app\common\services;

use think\config;

use think\Env;

class CodeMaServices
{
    public $appid;

    public $appsecret;

    public function __construct()
    {
        $this->appid = Env::get('XCXAPPKEY.appid') ;
        $this->appsecret = Env::get('XCXAPPKEY.appsecret') ;
    }

    /**
     *  获取access_token
     */
    public function getAccessToken()
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}";
        $request = $this->https_request($url);
        return $request['access_token'];
    }

    /**
     *  CURL模拟GET、POST请求
     */
    public function https_request($url, $data="")
    {
        //开启curl
        $ch = curl_init();
        //设置传输选项
        //设置传输地址
        curl_setopt($ch, CURLOPT_URL, $url);
        //以文件流的形式返回
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if ($data) {
            //以POST方式
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //发送curl
        $request = curl_exec($ch);
        $tmpArr = json_decode($request, true);
        if (is_array($tmpArr)) {
            return $tmpArr;
        } else {
            return $request;
        }
        //关闭资源
        curl_close($ch);
    }
}